Adjusting Menus
At any given time during the execution of your application, it's likely that some of the commands in your menus will not be appropriate. For example, if the front window is a dialog window, then any menu commands that manipulate only document windows should be disabled. Similarly, if the desktop shows no windows belonging to your application, then the Close command in the File menu should be disabled. When a menu item is disabled, it is drawn in a dimmed text and is not highlighted when the cursor passes over it. This disabling prevents the user from choosing those commands.An easy way to achieve this effect is to call an application-defined routine that adjusts the menus according to the current application context just before you call either
MenuSelect
orMenuKey
. Listing 8-6 shows the version ofDoMenuAdjust
used by the Venn Diagrammer application.
PROCEDURE DoMenuAdjust; VAR myWindow: WindowPtr; myMenu: MenuHandle; count: Integer; BEGIN myWindow := FrontWindow; IF myWindow = NIL THEN DisableMenuItem(GetMenuHandle(mFile), iClose) ELSE EnableMenuItem(GetMenuHandle(mFile), iClose); myMenu := GetMenuHandle(mVennD); IF IsAppWindow(myWindow) THEN FOR count := 1 TO kNumTools DO EnableMenuItem(myMenu, count) ELSE FOR count := 1 TO kNumTools DO DisableMenuItem(myMenu, count); IF IsDAccWindow(myWindow) THEN EnableMenuItem(GetMenuHandle(mEdit), 0) ELSE DisableMenuItem(GetMenuHandle(mEdit), 0); DrawMenuBar; END;TheDoMenuAdjust
procedure callsFrontWindow
to get a pointer to the frontmost window belonging to the Venn Diagrammer application. If there is no window belonging to the Venn Diagrammer application,DoMenuAdjust
disables the Close menu command in the File menu. Conversely, if there is a window belonging to the application,DoMenuAdjust
enables the Close command.If the front window is a document window, then
DoMenuAdjust
enables all the document-specific commands in the Venn menu; otherwise, it disables all those commands. (DoMenuAdjust
retrieves the menu handle by callingGetMenuHandle
and passes that handle toEnableMenuItem
orDisableMenuItem
.)You can disable or enable an entire menu by passing
DisableMenuItem
orEnableMenuItem
the value 0 in place of a menu item number. This is the strategy thatDoMenuAdjust
follows for the Edit menu. Venn Diagrammer does no editing of its own, soDoMenuAdjust
makes certain to enable the Edit menu only when a desk accessory window is frontmost. When you callDisableMenuItem
orEnableMenuItem
in this way, however, you also need to call the Menu Manager procedureDrawMenuBar
to update the menu bar's appearance.